import * as THREE from "three";
import scene from "../scene";
import World from './World'
import Light from './light'
import monitor from './monitor'
import WorldPlane from "../cannon/WorldPlane"
import mitt from "@/utils/mitt";
import controls from "../controls";
import * as CANNON from "cannon-es";
import WorldCar from "../cannon/WorldCar";
import gsap from "gsap";
export default function createCity () {
  const intersectGsap = {
    obj: null,
    in: true,
    out: true
  }
  // 将几何体添加到场景中
  new World().getmesh().then((gltf) => {
    gltf.scene.traverse((child) => {
      if (child.name === '平面') {
        // 找到具有特定名称的物体
        child.receiveShadow = true
        // WorldPlane.worldmesh.position.y = child.position.y
      }
      if (child.name === '文本') {
        // 找到具有特定名称的物体
        child.castShadow = true
      }
      if (child.name === '车' || child.name.includes('轮子')) {
        // 找到具有特定名称的物体transparent
        child.material.depthWrite = true
        child.material.normalScale = new THREE.Vector2(1, 1)
        child.material.side = THREE.FrontSide;
        child.material.vertexColors = false
        child.material.transparent = false
        // 创建射线投射器
        const raycaster = new THREE.Raycaster();
        // 从车的位置向下发射射线
        const rayDirection = new THREE.Vector3(0, -1, 0); // 向下发射射线
        let intersect = null
        let EnterImg = null
        child.name === '车' && mitt.on('time', () => {
          raycaster.set(child.position.clone(), rayDirection);
          const intersects = raycaster.intersectObjects(monitor.pedalOBJ);
          const obj = intersects.find(item => item.object.name.includes('平面-'))
          if (EnterImg) EnterImg.visible = EnterImg.position.y > 0
          // 计算射线和平面的相交情况
          if (intersects.length > 0 && obj) {
            // 如果有相交，表示车碰到了平面
            intersect = monitor.zebraCrossingOBJ.find(item => item.name.includes(obj.object.name.split('-')[1]))
            gsap.to(intersect.position, {
              y: "1",
              ease: "linear",
              duration: 0.8,
            })
            EnterImg = monitor.EnterImgOBJ.find(item => item.name.includes(obj.object.name.split('-')[1]))
            gsap.to(EnterImg.position, {
              y: "2",
              ease: "linear",
              duration: 0.8,
            })

            // 可以在这里执行碰撞后的操作
          } else {
            intersect && gsap.to(intersect.position, {
              y: "-1",
              ease: "linear",
              duration: 1,
            })
            EnterImg && gsap.to(EnterImg.position, {
              y: "-1",
              ease: "linear",
              duration: 1,
            })
          }
        })
      }
    });
    new WorldCar(gltf.scene.children)
    scene.add(gltf.scene);

  })

  //添加灯光
  const { ambientLight, spotLight } = Light


  scene.add(ambientLight);
  scene.add(spotLight)

}
